<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="modelName">
              <el-input v-model="filters.modelName" clearable placeholder="模板名称"></el-input>
            </el-form-item>
            <el-form-item prop="modelType">
              <el-select v-model="filters.modelType" clearable placeholder="模板类型">
                <el-option v-for="item in modelTypeList" :key="item.value" :value="item.value" :label="item.label"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="streetIds">
              <Region ref="region" :streetValues="filters.streetIds" @regionChange="handleItemChange" :isChangeOnSelect="true"></Region>
            </el-form-item>
            <el-form-item prop="villageId">
              <el-select v-model="filters.villageId" clearable filterable @focus="handleVillageFocus" placeholder="请选择小区">
                <el-option v-for="item in villageList" :value="item.id" :label="item.name" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="defaultFlag">
              <el-checkbox v-model="filters.defaultFlag">默认模板</el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增 </el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column align="center" label="模板名称" prop="modelName" width="220px"> </el-table-column>
        <el-table-column align="center" label="模板类型" width="100px">
          <template slot-scope="scope">
            <div v-if="scope.row.modelType == 1">常规版</div>
            <div v-if="scope.row.modelType == 2">自由版</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否默认模板" width="100">
          <template slot-scope="scope">
            <div>
              <el-tag size="mini" effect="dark" v-if="scope.row.defaultFlag == 1"> 是 </el-tag>
              <el-tag size="mini" effect="dark" v-if="scope.row.defaultFlag == 0" type="info"> 否 </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="地址" prop="address"> </el-table-column>
        <el-table-column label="状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="创建人" prop="createUserName"> </el-table-column>
        <el-table-column align="center" width="150" label="创建时间" prop="createDate"> </el-table-column>
        <el-table-column align="center" width="120" label="修改人" prop="modifyUserName"> </el-table-column>
        <el-table-column align="center" width="150" label="修改时间" prop="modifyDate"> </el-table-column>

        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑 </el-dropdown-item>
                <el-dropdown-item v-has="permissionPromoteModel" @click.native="handleRelateProModel(scope.row)">关联推广模板</el-dropdown-item>
                <el-dropdown-item v-has="permissionCheck" @click.native="handleClickBandBeeper(scope.row)">绑定呼叫器 </el-dropdown-item>
                <el-dropdown-item v-has="permissionCopy" @click.native="handleClickCopyTemplate(scope.row)">复制服务模版 </el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)"> {{ scope.row.state === 0 ? '启用' : '禁用' }}</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除 </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <BindBeeper :show="bandBeeperParam.show" :id="bandBeeperParam.id" @cancel="cancelBandBeeper" />
    <PromoteModel :show="relateProModelParam.show" :id="relateProModelParam.id" @confirm="confirmProModel" @cancel="relateProModelParam.show = false"></PromoteModel>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import VillageApi from '@/api/address/village';
import ServerTemplateApi from '@/api/server/serverTemplate';
import Region from '@/components/Region';
import BindBeeper from '../../components/BindBeeper.vue';
import PromoteModel from '../../components/PromoteModel.vue';
export default {
  name: 'ServerTemplateManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        modelName: '',
        modelType: '',
        defaultFlag: false,
        streetIds: [],
        provinceId: '',
        cityId: '',
        areaId: '',
        streetId: '',
        villageId: '',
      },
      modelTypeList: [
        { value: 1, label: '常规版' },
        { value: 2, label: '自由版' },
      ],
      villageList: [],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      bandBeeperParam: {
        show: false,
        id: '',
      },
      relateProModelParam: {
        show: false,
        id: '',
      },

      // 权限

      permissionAdd: 'SYSTEM_MANAGER_SERVER_TEMPLATE_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_SERVER_TEMPLATE_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_SERVER_TEMPLATE_LIST_EDIT',
      permissionUpStatus: 'SYSTEM_MANAGER_SERVER_TEMPLATE_LIST_STATE',
      permissionDelete: 'SYSTEM_MANAGER_SERVER_TEMPLATE_LIST_DELETE',
      permissionPromoteModel: 'SYSTEM_MANAGER_SERVER_TEMPLATE_LIST_PROMOTEMODEL',
      permissionCopy: 'SYSTEM_MANAGER_SERVER_TEMPLATE_LIST_COPY',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {
    Region,
    BindBeeper,
    PromoteModel,
  },
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    formatState(cellValue) {
      if (cellValue === -1) {
        return '删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue === -1) {
        return 'danger';
      } else if (cellValue === 0) {
        return 'info';
      } else if (cellValue === 1) {
        return 'success';
      }
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.filters.provinceId = '';
      this.filters.cityId = '';
      this.filters.areaId = '';
      this.filters.streetId = '';
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    handleItemChange(val) {
      this.villageList = [];
      this.filters.provinceId = val.provinceId;
      this.filters.cityId = val.cityId;
      this.filters.areaId = val.areaId;
      this.filters.streetId = val.streetId;
      if (val.provinceId) {
        this.filters.streetIds = [val.provinceId, val.cityId, val.areaId, val.streetId];
      } else {
        this.filters.streetIds = [];
      }
    },
    //选择小区
    handleVillageFocus() {
      if (this.filters.streetIds.length > 0) {
        let param = {
          provinceId: this.filters.provinceId,
          cityId: this.filters.cityId,
          areaId: this.filters.areaId,
          streetId: this.filters.streetId,
          name: '',
        };
        VillageApi.villageList(param).then((res) => {
          this.villageList = res.data;
        });
      }
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        modelName: _this.filters.modelName,
        modelType: _this.filters.modelType,
        defaultFlag: _this.filters.defaultFlag ? 1 : '',
        provinceId: _this.filters.provinceId,
        cityId: _this.filters.cityId,
        areaId: _this.filters.areaId,
        streetId: _this.filters.streetId,
        villageId: _this.filters.villageId,
      };
      // 发送请求
      ServerTemplateApi.listServerTemplate(cond)
        .then((res) => {
          _this.listLoading = false;
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
    handleRelateProModel(row) {
      this.relateProModelParam.id = row.modelId;
      this.relateProModelParam.show = true;
    },
    confirmProModel() {
      this.relateProModelParam.show = false;
    },
    handleIsDisable(row) {
      let currentState = row.state === 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        ServerTemplateApi.deleteServerTemplate({
          id: row.modelId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该模板', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.modelId,
          state: -1,
        };
        ServerTemplateApi.deleteServerTemplate(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/server/serverTemplateDetail',
        query: { id: row.modelId },
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/server/serverTemplateEdit',
        query: { id: val.modelId, isCopy: 0 },
      });
    },
    handleClickCopyTemplate(val) {
      let _this = this;
      _this.$router.push({
        path: '/server/serverTemplateEdit',
        query: { id: val.modelId, isCopy: 1 },
      });
    },
    handleClickAdd() {
      // 刷新
      let _this = this;
      _this.$router.push('/server/serverTemplateAdd');
    },
    handleClickBandBeeper(row) {
      this.bandBeeperParam.id = row.modelId;
      this.bandBeeperParam.show = true;
    },
    cancelBandBeeper() {
      this.bandBeeperParam.show = false;
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
